package top.hjcwzx.wrench.limiter.strategy.key;

import org.springframework.stereotype.Component;
import top.hjcwzx.wrench.limiter.enums.KeyStrategyEnum;
import top.hjcwzx.wrench.limiter.strategy.key.impl.CommonKeyStrategy;
import top.hjcwzx.wrench.limiter.strategy.key.impl.SpelKeyStrategy;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//策略选择上下文
public class RateLimiterKeyStrategyContext {

    private final Map<KeyStrategyEnum, RateLimiterKeyStrategy> keyGroup = new ConcurrentHashMap<KeyStrategyEnum, RateLimiterKeyStrategy>();

    @PostConstruct
    public void init() {
        keyGroup.put(KeyStrategyEnum.COMMON, new CommonKeyStrategy());
        keyGroup.put(KeyStrategyEnum.SPEL, new SpelKeyStrategy());
    }

    public RateLimiterKeyStrategy getKeyStrategy(KeyStrategyEnum keyStrategy) {
        RateLimiterKeyStrategy strategy = keyGroup.get(keyStrategy);
        return null == strategy ? keyGroup.get(KeyStrategyEnum.COMMON) : strategy;
    }
}
